<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="产品编号" min-width="150">
        <template #default="{ row }">
          <!-- <el-form-item :prop="`${$index}.productId`"  class="mb-0px!">
            <el-input v-model="row.productId" placeholder="请输入产品编号" :readonly="true" />
          </el-form-item> -->
          {{row.productId}}
        </template>
      </el-table-column>
      <el-table-column label="产品名称" min-width="150">
        <template #default="{ row }">
        
          {{row.productName}}
        </template>
      </el-table-column>
      <el-table-column label="产品单位" min-width="150">
        <template #default="{ row }">
          {{row.productUnitName}}
        </template>
      </el-table-column>
      <el-table-column label="产品规格" min-width="150">
        <template #default="{ row }">
        
          {{row.productStandards}}
        </template>
      </el-table-column>
      <el-table-column label="产品单价" min-width="150">
        <template #default="{ row }">
        
          {{row.productPrice}}
        </template>
      </el-table-column>
      <el-table-column label="数量" min-width="150">
        <template #default="{ row }">
        
          {{row.count}}
        </template>
      </el-table-column>
      <el-table-column label="总价" min-width="150">
        <template #default="{ row }">
       
          {{row.totalPrice}}
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row }">
         
          {{row.remark}}
        </template>
      </el-table-column>
     
    </el-table>
  </el-form>
  <!-- <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加ERP 采购单项</el-button>
  </el-row> -->
</template>
<script setup lang="ts">
import { PurPurchaseOrderApi } from '@/api/erp/purpurchaseorder'

const props = defineProps<{
  purchaseOrderId: undefined // 采购单编号-提交供应商采购单编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  purchaseOrderId: [{ required: true, message: '采购单编号-提交供应商采购单编号不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '产品编号不能为空', trigger: 'blur' }],
  productUnit: [{ required: true, message: '产品单位不能为空', trigger: 'change' }],
  productStandards: [{ required: true, message: '产品规格不能为空', trigger: 'blur' }],
  productPrice: [{ required: true, message: '产品单价不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
  totalPrice: [{ required: true, message: '总价不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.purchaseOrderId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await PurPurchaseOrderApi.getPurPurchaseOrderItemListByPurchaseOrderId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    purchaseOrderId: undefined,
    productId: undefined,
    productUnit: undefined,
    productStandards: undefined,
    productPrice: undefined,
    count: undefined,
    totalPrice: undefined,
    remark: undefined,
  }
  row.purchaseOrderId = props.purchaseOrderId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>